<?
	function function1($args)
	{
		return 'ok';
	}

	class cls
	{
		function index()
		{
			print_r($this);
		}
	}

	class c_test_controller extends c_base_controller
	{
		function index()
		{
			global $mvc;
			
			$ins = new c_insertion_util();
			
			$ins->insertions['func'] = array($this, 'func');
			$ins->insertions['sub_w_ins'] = '<? sub=<<ins: tpl=`<{row.name}>`>> ?>';
			$ins->insertions['sub'] = '<? $this->vars["row"] = 100; {{tpl}} ?>';
			
			$ins->insertions['sql'] = '<? $r = $mvc->db->query({{query}}); while($a = mysql_fetch_assoc($r)) { $this->vars["row"] = $a; {{tpl}} ?>';
			
			$ins->insertions['arr'] = '<? $data=array(0, 1, 2); foreach($data as $row) { $this->vars["row"] = $row; {{tpl}} } ?>';
			$ins->insertions['ins2'] = '(ins2_x:{{x}}){{x}}';
			$ins->insertions['ins1'] = '(prev_ins1_x:{{x}})<<ins2: x=`10`>>(post_ins1_x:{{x}})';
			
			
			echo '<pre>';
			
			$ins->insertions['sql'] = '<?
				global $mvc;
								
				$r = $mvc->db->query($this->parse($this->v("query")));				
				while($a = mysql_fetch_assoc($r))
				{
					$this->vars["row"] = $a;
					echo $this->parse($this->vars["tpl"]);
				}
				mysql_free_result($r);
				?>';
			

			// Вывести двумерный массив.
			$ins->vars['var'] = 200;
			$ins->vars['x'] = 20;
			
			//$text = '<<sql: tpl=`<b><{row.id}></b><br>`>>{{var}}';			
			//$text = '<<sub: tpl=`<b><{row}></b><br>`>>{{var}}';
			//$text = '(prev_x:{{x}})<<ins1: x=`5`>>(post_x:{{x}})';
			//$text = '<<arr: tpl=`<i><{row}></i> `>>';
			$ins->vars['rowid'] = 10;
			$text = '<<sql: query=`SELECT * FROM workers WHERE id={{rowid}}` tpl=`"<i>".$row["id"]." - ".$row["fio"]."</i><br>"`>>';

			$text = '
				<<sql: query=`SELECT * FROM workers WHERE id > {{rowid}}`
					tpl=`<b><{row.id}></b>
						<<sql: query=`SELECT * FROM workers WHERE id > <{parent.row.id}>`
							tpl=`<i><{row.id}> - <{row.fio}></i><br>`
						>>`
				>>';
			
			$mvc->bm->add('start2');						
			
			$ins->debug = true;
			echo $ins->parse($text);
			
			print_r($mvc->messages);
			
			$mvc->bm->add('end');
			
			
			include ($mvc->view_path.'bm_default.php');
			echo '<pre>';
			//print_r($mvc->bm->points, true);
			print_r($ins->report);
			
			die();
			
			
			//new cls();
			//::index();
			
			//die();
			
			
			
			//$mvc->add_hook('e_test', 'function1');
			//$mvc->add_hook('e_test2', 'c_default_controller.index');
			//$mvc->del_hook('e_test', 'function1');

			//echo $mvc->emit('e_test', array(0, 1, 2));

			//die();
			echo '<pre>';
			$data = $mvc->db->get_data('SELECT * FROM workers');
			$this->attach($data, 'workplaces');
			
			print_r($data);
		
			//$this->build('order->(leader->(workplace->workplaces), lifters->(workplace->workplaces))');
		
			return '<br><br><hr>Hallo world :)';
		}
		
		function func($params, $parser)
		{
			return $params['id'];
		}
		
		function attach(&$data, $table)
		{
			global $mvc;
			
			foreach($data as $k => $row)
			{
				$sql = 'SELECT * FROM '.$table.' WHERE id in ('.$row['workplace'].')';
				$data[$k]['workers'] = $mvc->db->get_data($sql);
			}			
		}
		
		function build($struct)
		{
			print_r($struct);
		}
	}
?>